import api from "api/api"
import cookie from "js-cookie"
import Vue from 'vue'
import wx from 'jweixin-wechat'
import { Toast } from 'vant'
// 长按指令
Vue.directive('longpress', {
  bind: function (el, binding, vNode) {
    if (typeof binding.value !== 'function') {
      // Fetch name of component
      const compName = vNode.context.name
      // pass warning to console
      let warn = `[longpress:] provided expression '${binding.expression}' is not a function, but has to be`
      if (compName) { warn += `Found in component '${compName}' ` }
    }
    let pressTimer = null
    let start = (e) => {
      if (e.type === 'click' && e.button !== 0) {
        return;
      }
      if (pressTimer === null) {
        pressTimer = setTimeout(() => {
          handler()
        }, 1000)
      }
    }
    // Cancel Timeout
    let cancel = (e) => {
      // Check if timer has a value or not
      if (pressTimer !== null) {
        clearTimeout(pressTimer)
        pressTimer = null
      }
    }
    // Run Function
    const handler = (e) => {
      binding.value(e)
    }
    // Add Event listeners
    el.addEventListener("mousedown", start);
    el.addEventListener("touchstart", start);
    // Cancel timeouts if this events happen
    el.addEventListener("click", cancel);
    el.addEventListener("mouseout", cancel);
    el.addEventListener("touchend", cancel);
    el.addEventListener("touchcancel", cancel);
  }
})
Vue.directive('drag', {
  bind: function (el) {
    let odiv = el;   //获取当前元素
    oDiv.onmousedown = (e) => {
      //算出鼠标相对元素的位置
      let disX = e.clientX - odiv.offsetLeft;
      let disY = e.clientY - odiv.offsetTop;

      document.onmousemove = (e) => {
        //用鼠标的位置减去鼠标相对元素的位置，得到元素的位置
        let left = e.clientX - disX;
        let top = e.clientY - disY;

        //绑定元素位置到positionX和positionY上面
        this.positionX = top;
        this.positionY = left;

        //移动当前元素
        odiv.style.left = left + 'px';
        odiv.style.top = top + 'px';
      };
      document.onmouseup = (e) => {
        document.onmousemove = null;
        document.onmouseup = null;
      };
    };
  }
})

// 获取链接参数
export const GetSearchParam = (name) => {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  var r = window.location.search.substr(1).match(reg);
  if (r != null) return (r[2]); return null;
}
// 随机整数
export const random = (min, max) => {
  return parseInt(Math.random() * (max - min + 1) + min, 10);
}
// 进入后台
export const getHiddenProp = () => {
  const prefixes = ['webkit', 'moz', 'ms', 'o'];
  if ('hidden' in document) return 'hidden';
  for (var i = 0; i < prefixes.length; i++) {
    if ((prefixes[i] + 'Hidden') in document)
      return prefixes[i] + 'Hidden';
  }
  return null;
}
export const getVisibilityState = () => {
  const prefixes = ['webkit', 'moz', 'ms', 'o'];
  if ('visibilityState' in document) return 'visibilityState';
  for (var i = 0; i < prefixes.length; i++) {
    if ((prefixes[i] + 'VisibilityState') in document)
      return prefixes[i] + 'VisibilityState';
  }
  return null;
}
// 判断浏览器
export const getBroswer = () => {
  var u = navigator.userAgent, app = navigator.appVersion;
  return {//移动终端浏览器版本信息
    trident: u.indexOf('Trident') > -1, //IE内核
    presto: u.indexOf('Presto') > -1, //opera内核
    webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
    gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
    mobile: !!u.match(/AppleWebKit.*Mobile.*/)
      || !!u.match(/AppleWebKit/), //是否为移动终端
    ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
    android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或者uc浏览器
    iPhone: u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1, //是否为iPhone或者QQHD浏览器
    iPad: u.indexOf('iPad') > -1, //是否iPad
    webApp: u.indexOf('Safari') == -1,//是否web应该程序，没有头部与底部
    google: u.indexOf('Chrome') > -1,
    weChat: (u.indexOf('MicroMessenger') > -1 || u.indexOf('micromessenger') > -1) && u.indexOf('wxwork') === -1
  };
}

export const isPc = () => {
  // 跳pc
  const userAgentInfo = navigator.userAgent;
  const Agents = new Array("Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod");
  let flag = true;
  for (let v = 0; v < Agents.length; v++) {
    if (userAgentInfo.indexOf(Agents[v]) > -1) {
      flag = false;
      break;
    }
  }
  return flag
}

export const isMiniapp = () => {
  return navigator.userAgent.toLowerCase().indexOf('miniprogram') > -1;
}
export function isPhone (str) {
  const reg = /^1\d{10}$/
  if (str === '') {
    return false
  } else {
    return reg.test(str.trim())
  }
}

export function fixIosInputBug () {
  document.body.scrollTop = document.body.scrollTop;
  document.documentElement.scrollTop = document.documentElement.scrollTop;
}

// 埋点
export function visitRecord (key, param) {
  api.visitRecord({
    key: key,
    params: param,
    activity_no: window.location.pathname.split('/').slice(-1)[0]
  }).then(res => {

  })
}
// 图片埋点
export function clickRecord (value, hash) {
  api.clickRecord({ key: value, params: hash, activity_no: window.location.pathname.split('/').slice(-1)[0] })
}

export function softHeight (a) {
  let winW = window.innerWidth
  return winW / 750 * a;
}
// 换行
export function replaceReturn (str) {
  var regRN = /(\r\n)|(\n)/g;
  if (typeof (str) != 'string') {
    return str
  }
  str = str.replace(regRN, '<br>');
  return str;
}
// 将<br>换回\n;
export function backReturn (str) {
  var regRN = /<br>/g;
  str = str.replace(regRN, '\n');
  return str;
}

// 节流
export function throttle (method, delay, time) {
  var timeout, startTime = new Date();
  return function () {
    var context = this,
      args = arguments,
      curTime = new Date();
    clearTimeout(timeout);
    // 如果达到了规定的触发时间间隔，触发 handler
    if (curTime - startTime >= time) {
      method.apply(context, args);
      startTime = curTime;
      // 没达到触发间隔，重新设定定时器
    } else {
      timeout = setTimeout(method, delay);
    }
  }
}
// 回到顶部
export function toTop (id) {
  let ele = document.getElementById(id) || document.body;
  let timer = null;
  let max = 0;
  cancelAnimationFrame(timer);
  timer = requestAnimationFrame(function fn () {
    let oTop = ele.scrollTop || (document.body.scrollTop || document.documentElement.scrollTop);
    max = oTop > max ? oTop : max;
    let step = max / 30;
    if (oTop > 0) {
      if (id) {
        ele.scrollTop = oTop - step;
      } else {
        document.body.scrollTop = document.documentElement.scrollTop = oTop - step;
      }
      timer = requestAnimationFrame(fn);
    } else {
      cancelAnimationFrame(timer);
    }
  });
}

export function toBottom (st) {
  let timer = null;
  let max = 0;
  cancelAnimationFrame(timer);
  timer = requestAnimationFrame(function fn () {
    let oTop = document.body.scrollTop || document.documentElement.scrollTop;
    max = oTop >= max ? oTop : max;
    let step = st / 30;
    if (oTop < st) {
      document.body.scrollTop = document.documentElement.scrollTop = oTop + step;
      timer = requestAnimationFrame(fn);
    } else {
      cancelAnimationFrame(timer);
    }
    let endSt = document.body.scrollTop || document.documentElement.scrollTop;
    if (endSt === oTop) {
      cancelAnimationFrame(timer);
    }
  });
}
// 音乐自动播放
export function audioAutoPlay (id) {
  var audio = document.getElementById(id);
  audio.play();
  function audioInBroswer () {
    audio.play();
    document.removeEventListener('touchstart', audioInBroswer);
  }
  document.addEventListener('touchstart', audioInBroswer);
  document.addEventListener('WeixinJSBridgeReady', function () {
    audio.play();
  }, false);
}
// 请在微信客户端打开
export function pleaseOpenWx () {
  const isWx = getBroswer().weChat;
  if (!isWx) {
    Toast(window.i18n.t('common.openinwx'));
    return true
  }
  return false
}
// 压缩图片
export function compress (fileObj) { // 文件的对象 Blob
  let reader = new FileReader()
  reader.readAsDataURL(fileObj)

  return new Promise(function (resolve, reject) {
    reader.onload = function (e) {
      let image = new Image() //新建一个img标签（还没嵌入DOM节点)
      image.src = e.target.result
      image.onload = function () {
        let canvas = document.createElement('canvas')
        let context = canvas.getContext('2d')

        let w = image.width
        let h = image.height
        let maxSide = Math.max.apply(null, [w, h])
        if (maxSide > 1000) { // 限制最大长边1000
          let scale = maxSide / 1000
          w = w / scale
          h = h / scale
        }

        let data = ''

        canvas.width = w
        canvas.height = h

        context.drawImage(image, 0, 0, w, h)
        data = canvas.toDataURL('image/jpeg')

        resolve(data)
        //压缩完成
        // return data
      }
    }
  })

}
// base64转Blob
export function dataURLtoBlob (dataurl) {
  var arr = dataurl.split(','),
    mime = arr[0].match(/:(.*?);/)[1],
    bstr = atob(arr[1]),
    n = bstr.length,
    u8arr = new Uint8Array(n);
  while (n--) {
    u8arr[n] = bstr.charCodeAt(n);
  }
  return new Blob([u8arr], { type: mime });
}

export function objKeySort (obj) { // key按字典序
  var newkey = Object.keys(obj).sort();
  //先用Object内置类的keys方法获取要排序对象的属性名，再利用Array原型上的sort方法对获取的属性名进行排序，newkey是一个数组
  var newObj = ''; //创建一个新的字符串，用于存放排好序的键值对
  for (var i = 0; i < newkey.length; i++) { //遍历newkey数组
    if (obj[newkey[i]] === null || obj[newkey[i]] === undefined) continue
    obj[newkey[i]] = JSON.stringify(obj[newkey[i]]);
    //向新创建的对象中按照排好的顺序依次增加键值对
    newObj += ((newObj.indexOf('=') != -1) ? '&' : '') + newkey[i] + '=' + obj[newkey[i]];
  }
  return newObj;
}
